{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-02T06:38:27.971507Z",
     "start_time": "2025-11-02T06:38:27.967641Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "data = {\n",
    "    '城市': ['北京', '上海', '广州', '北京', '广州', '上海', '北京'],\n",
    "    '销售额': [100, 200, 150, 80, 120, 250, 90],\n",
    "    '月份': ['一月', '一月', '一月', '二月', '二月', '二月', '三月']\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "print(df)\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   城市  销售额  月份\n",
      "0  北京  100  一月\n",
      "1  上海  200  一月\n",
      "2  广州  150  一月\n",
      "3  北京   80  二月\n",
      "4  广州  120  二月\n",
      "5  上海  250  二月\n",
      "6  北京   90  三月\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:38:27.997253Z",
     "start_time": "2025-11-02T06:38:27.993509Z"
    }
   },
   "cell_type": "code",
   "source": [
    "grouped = df.groupby('城市')['销售额'].sum()\n",
    "print(grouped)"
   ],
   "id": "997803eb46f18db2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "城市\n",
      "上海    450\n",
      "北京    270\n",
      "广州    270\n",
      "Name: 销售额, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:38:28.011610Z",
     "start_time": "2025-11-02T06:38:28.007694Z"
    }
   },
   "cell_type": "code",
   "source": [
    "grouped_multi = df.groupby(['城市', '月份'])['销售额'].sum()\n",
    "print(grouped_multi)"
   ],
   "id": "f9e10783e162f8c0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "城市  月份\n",
      "上海  一月    200\n",
      "    二月    250\n",
      "北京  一月    100\n",
      "    三月     90\n",
      "    二月     80\n",
      "广州  一月    150\n",
      "    二月    120\n",
      "Name: 销售额, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:38:28.027153Z",
     "start_time": "2025-11-02T06:38:28.022300Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for city, group in df.groupby('城市'):\n",
    "    print(f\"城市: {city}\")\n",
    "    print(group)"
   ],
   "id": "fe6167a1166e6b74",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "城市: 上海\n",
      "   城市  销售额  月份\n",
      "1  上海  200  一月\n",
      "5  上海  250  二月\n",
      "城市: 北京\n",
      "   城市  销售额  月份\n",
      "0  北京  100  一月\n",
      "3  北京   80  二月\n",
      "6  北京   90  三月\n",
      "城市: 广州\n",
      "   城市  销售额  月份\n",
      "2  广州  150  一月\n",
      "4  广州  120  二月\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:38:28.055501Z",
     "start_time": "2025-11-02T06:38:28.043664Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 数据创建\n",
    "data = {\n",
    "    '城市': ['北京', '上海', '广州', '北京', '广州', '上海', '北京'],\n",
    "    '销售额': [100, 200, 150, 80, 120, 250, 90],\n",
    "    '月份': ['一月', '一月', '一月', '二月', '二月', '二月', '三月']\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "# 标准化函数，保持与原始索引一致\n",
    "def standardize(group):\n",
    "    group['标准化销售额'] = (group['销售额'] - group['销售额'].mean()) / group['销售额'].std()\n",
    "    return group\n",
    "\n",
    "# 使用 apply，并将结果重新索引回原始 DataFrame\n",
    "df = df.groupby('城市').apply(standardize).reset_index(drop=True)\n",
    "print(df)"
   ],
   "id": "79cf9b23746a6c27",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   城市  销售额  月份    标准化销售额\n",
      "0  上海  200  一月 -0.707107\n",
      "1  上海  250  二月  0.707107\n",
      "2  北京  100  一月  1.000000\n",
      "3  北京   80  二月 -1.000000\n",
      "4  北京   90  三月  0.000000\n",
      "5  广州  150  一月  0.707107\n",
      "6  广州  120  二月 -0.707107\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\l1751\\AppData\\Local\\Temp\\ipykernel_54552\\1495961850.py:16: DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.\n",
      "  df = df.groupby('城市').apply(standardize).reset_index(drop=True)\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:38:28.069834Z",
     "start_time": "2025-11-02T06:38:28.068108Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "3f021bb85efdcc59",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
